package com.dtdream.emr;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class RegexpCount extends UDF {

    public int evaluate(String source, String pattern) {
        if (null == source || null == pattern){
            return 0;
        }

        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(source);
        int count = 0;
        while (m.find()) {
            count++;
        }
        return count;
    }

    public int evaluate(String source, String pattern, int startp) {
        if (null == source || null == pattern){
            return 0;
        }

        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(source.substring(startp));
        int count = 0;

        while (m.find()) {
            count++;
        }

        return count;
    }

}



